package biz.inv.mgbean;

import biz.common.service.ProdLocationSvr;
import biz.inv.dto.StockIn;
import biz.inv.dto.StockInDetail;
import biz.inv.dto.StockTransSearch;
import biz.inv.service.StockInSvr;
import biz.master.dto.*;
import biz.master.service.ProductSvr;
import biz.master.service.SupplierSvr;
import glen.util.BaseManageBean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Test on 2/14/14.
 */
@ManagedBean(name = "stockInMgb")
@ViewScoped
public class StockInMgb extends BaseManageBean<StockIn, StockInSvr> {


    private List<StockInDetail> itemDetails;

    private StockInDetail newDetail = new StockInDetail();
    private transient ProdLocationSvr prodLocationSvr = new ProdLocationSvr();
    private String searchDocNo;
    private String searchLotNo;
    private Date searchDateFrom;
    private Date searchDateTo;

    public StockInMgb() throws InstantiationException, IllegalAccessException {
        super(StockIn.class, StockInSvr.class);

    }

    public void add() {
        selected = new StockIn();

        itemDetails = new ArrayList<>();
        newDetail = new StockInDetail();
    }

    public void view(StockIn row) {
        selected = row;
        itemDetails = svr.searchStockInDetail(row.getId());
        newDetail = new StockInDetail();

    }

    public void reset() {
        resultSearch = null;
        searchDateFrom = null;
        searchDateTo = null;
        searchDocNo = null;
        searchLotNo = null;
    }

    public String getSearchDocNo() {
        return searchDocNo;
    }

    public void setSearchDocNo(String searchDocNo) {
        this.searchDocNo = searchDocNo;
    }

    public String getSearchLotNo() {
        return searchLotNo;
    }

    public void setSearchLotNo(String searchLotNo) {
        this.searchLotNo = searchLotNo;
    }

    public Date getSearchDateFrom() {
        return searchDateFrom;
    }

    public void setSearchDateFrom(Date searchDateFrom) {
        this.searchDateFrom = searchDateFrom;
    }

    public Date getSearchDateTo() {
        return searchDateTo;
    }

    public void setSearchDateTo(Date searchDateTo) {
        this.searchDateTo = searchDateTo;
    }

    public void removeItem(StockInDetail item) {
        itemDetails.remove(item);
    }

    public void search() {
        StockTransSearch o = new StockTransSearch();
        o.setDocDateFrom(searchDateFrom);
        o.setDocDateTo(searchDateTo);
        o.setDocNo(searchDocNo);
        o.setLotNo(searchLotNo);
        resultSearch = svr.search(o);
    }

    public void save() {
        svr.save(selected, itemDetails);
    }

    public List<StockInDetail> getItemDetails() {
        return itemDetails;
    }

    public void setItemDetails(ArrayList<StockInDetail> itemDetails) {
        this.itemDetails = itemDetails;
    }

    public StockInDetail getNewDetail() {
        return newDetail;
    }

    public void setNewDetail(StockInDetail newDetail) {
        this.newDetail = newDetail;
    }

    public void addDetail() {
        if (itemDetails == null)
            itemDetails = new ArrayList<>();
        Product product = (new ProductSvr()).get(newDetail.getProduct().getId());
        StockInDetail detail = new StockInDetail();
        detail.setQty(newDetail.getQty());
        detail.setProduct(product);
        itemDetails.add(detail);
        newDetail = new StockInDetail();
    }

    public List<Supplier> getSupplierList() {
        return (new SupplierSvr()).search(null);
    }

    public List<Product> getProductList() {

        return (new ProductSvr()).search(null);
    }

    public void changeProdLocation() {

    }

    public List<Area> getAreaList() {
        return prodLocationSvr.searchArea(null);
    }

    public List<Branch> getBranchList() {
        return prodLocationSvr.searchBranchByArea(selected.getAreaId());
    }

    public List<Warehouse> getWarehouseList() {
        return prodLocationSvr.searchWarehouseByBranch(selected.getBranchId());
    }

    public List<Location> getLocationList() {
        return prodLocationSvr.searchLocationByWarehouse(selected.getWarehouseId());
    }
}
